﻿using System;
using System.Globalization;
using System.Web.Security;
using Monkey.Framework.Logging;
using QuanLyHopDong.Biz;
using QuanLyHopDong.Biz.Model;
using QuanLyHopDong.Web.Common;

namespace QuanLyHopDong.Web.Employees
{
    public partial class New : System.Web.UI.Page
    {
        #region Properties

        private EmployeeBiz _employeeBiz;
        private EmployeeBiz EmployeeManager
        {
            get { return _employeeBiz ?? (_employeeBiz = new EmployeeBiz()); }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{
            //    // get auto code
            //    var nextCode = EmployeeManager.GetNextCode();
            //    txtCode.Text = string.Format("{0:00000}", nextCode);
            //    txtSortOrder.Text = nextCode.ToString(CultureInfo.InvariantCulture);
            //}
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;

            // validate inputs
            //var code = txtCode.Text.Trim();
            //if (code.Length == 0)
            //{
            //    lblErrorMessage.Text = Resources.GlobalStrings.CodeIsRequired;
            //    return;
            //}
            var name = txtName.Text.Trim();
            if (name.Length == 0)
            {
                lblErrorMessage.Text = Resources.GlobalStrings.NameIsRequired;
                return;
            }
            //var email = txtEmailAddress.Text.Trim();
            //var phone = txtPhoneNumber.Text.Trim();
            //var descrition = txtDescription.Text.Trim();
            //int sortOrder;
            //int.TryParse(txtSortOrder.Text.Trim(), out sortOrder);
            //var isHidden = chkHidden.Checked;
            var userName = txtUserName.Text.Trim();
            var password = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text, "SHA1");

            var employee = new Employee
            {
                Code = string.Empty,
                Name = name,
                UserName = userName,
                Password = password,
                //MailAddress = email,
                //PhoneNumber = phone,
                //Description = descrition,
                //SortOrder = sortOrder,
                Hidden = true
            };

            try
            {
                employee = EmployeeManager.CreateEmployee(employee);
            }
            catch (ApplicationException appEx)
            {
                lblErrorMessage.Text = WebCommon.GetGlobalString(appEx.Message);
                return;
            }
            catch(Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }

            if (employee == null)
            {
                lblErrorMessage.Text = Resources.GlobalStrings.CreateFailed;
                return;
            }

            // success
            Response.Redirect("List.aspx");
        }

        #endregion
    }
}